package com.tsd.base.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.tsd.base.entity.BaseCodeRuleItem;
import com.tsd.base.entity.BaseCodeRuleItemExt;
import com.tsd.base.service.BaseCodeRuleItemService;
import com.tsd.base.service.BaseCodeRuleService;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.HttpResult;
import com.tsd.system.controller.SystemBaseController;
import com.tsd.system.entity.SysUser;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：编码规则子表 控制层
 *
 * @author zsy
 * @date 2020/12/29 15:22:59
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
public class BaseCodeRuleItemController extends SystemBaseController {

    @Resource
    private BaseCodeRuleItemService baseCodeRuleItemService;
    @Resource
    private BaseCodeRuleService baseCodeRuleService;

    /**
     * 根据ID获取编码规则子表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getBaseCodeRuleItemById.action", method = RequestMethod.POST)
    public HttpResult<BaseCodeRuleItem> getBaseCodeRuleItemById() {
        HttpResult<BaseCodeRuleItem> hr = new HttpResult<BaseCodeRuleItem>();
        try {
            Map<String, Object> map = super.getParams(request);
            Long id = HlpUtils.getLong(map, "id");
            super.checkEmpty(id, "请提供参数id");
            BaseCodeRuleItem baseCodeRuleItem = baseCodeRuleItemService.getBaseCodeRuleItemById(id);
            hr.setData(baseCodeRuleItem);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    /**
     * 根据ID删除编码规则子表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteBaseCodeRuleItem.action", method = RequestMethod.POST)
    public HttpResult<String> deleteBaseCodeRuleItem() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Long id = HlpUtils.getLong(map, "id");
            super.checkEmpty(id, "请提供参数id");
            baseCodeRuleItemService.deleteBaseCodeRuleItem(id, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    /**
     * 保存编码规则子表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/saveBaseCodeRuleItem.action", method = RequestMethod.POST)
    public HttpResult<String> saveBaseCodeRuleItem() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            Map<String, Object> map = super.getParams(request);
            SysUser opUser = super.getSysUser();
            String json = JSON.toJSONString(map);
            BaseCodeRuleItem t = JSON.parseObject(json, BaseCodeRuleItem.class);
            baseCodeRuleItemService.saveBaseCodeRuleItem(t, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    /**
     * 批量保存编码规则子表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/batchSaveBaseCodeRuleItem.action", method = RequestMethod.POST)
    public HttpResult<String> batchSaveBaseCodeRuleItem() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            Map<String, Object> map = super.getParams(request);
            SysUser opUser = super.getSysUser();
            Object dataListObj = map.get("data_list");
            if (dataListObj instanceof List) {
                String json = JSON.toJSONString(dataListObj);
                List<BaseCodeRuleItemExt> list = JSONArray.parseArray(json, BaseCodeRuleItemExt.class);
                baseCodeRuleService.batchSaveBaseCodeRuleItem(list, opUser);
                hr.setCode(ErrorCode.CODE_SUCCESS);
            } else {
                throw new HlpException("data_list非数组格式");
            }
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    /**
     * 不分页获取编码规则子表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findBaseCodeRuleItems.action", method = RequestMethod.POST)
    public HttpResult<List<BaseCodeRuleItemExt>> findBaseCodeRuleItems() {
        HttpResult<List<BaseCodeRuleItemExt>> hr = new HttpResult<List<BaseCodeRuleItemExt>>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<String, Object>();
            HlpUtils.copyStringValue(map, params, "code_rule_sid", "code_rule_sid", true, false);
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            List<BaseCodeRuleItemExt> list = baseCodeRuleItemService.findBaseCodeRuleItems(params);
            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    /**
     * 批量删除编码规则子表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/batchDeleteBaseCodeRuleItem.action", method = RequestMethod.POST)
    public HttpResult<String> batchDeleteBaseCodeRuleItem() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Object idObject = map.get("id_list");
            super.checkEmpty(idObject, "请提供参数id_list");
            if (idObject instanceof List) {
                List<Long> idList = JSONArray.parseArray(JSON.toJSONString(idObject), Long.class);
                baseCodeRuleItemService.batchDeleteBaseCodeRuleItem(idList, opUser);
            } else {
                throw new HlpException("id_list非数组格式");
            }
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

}
